#include "wijiuiscriptable.h"
#include "WijiUi.h"
#include "wijiio.h"
#include "WijiDialog.h"
#include <QProcess>
#include <QFileDialog>
#include <stdio.h>
#include <WijiDOM.h>
WijiUiScriptable* WijiUiScriptable::_self = NULL;
WijiUiScriptable::WijiUiScriptable(QObject *parent)
: QObject(parent)
, scripter(parent) {

}
bool WijiUiScriptable::navigateTo(QString url) {
    bool result;
    WijiUi::getSelf()->webView->load(QUrl(url));
    return result;
}
QString WijiUiScriptable::parseFile(QString filename) {
	QString result = dom.parseFile(filename);
	return result;
}
QString WijiUiScriptable::parseHtml(QString html) {
    QString result = dom.parseHtml(html);
    return result;
}
//This way,  you can just type this.close(); from within QtScript.
void WijiUiScriptable::close() {
    QMetaObject::invokeMethod(WijiUi::getSelf(), "windowCloseRequested",
                              Qt::AutoConnection);
}
QString WijiUiScriptable::fileGetContents(QString filename) {
    QString contents = WijiIO::read(filename);
    return contents;
}
bool WijiUiScriptable::filePutContents(QString filename, QString contents, bool urlEncoded = true) {
    if (urlEncoded) {
        contents = QUrl::fromPercentEncoding(contents.toLatin1());
    }
    WijiIO::write(filename,contents);
    return true;
}
bool WijiUiScriptable::notify(QString title, QString message) {
    WijiUi::getSelf()->notify(title,message);
    return true;
}
QString WijiUiScriptable::getIniValue(QString key) {

    QString value = WijiSettings::getSelf()->getValue(key).toString();
    return value;
}

QString WijiUiScriptable::readFromProgram(QString line) {
    QString result;
    qDebug() << "readFromProgram called with: " << line;
    FILE *in;
    char buff[512];
    QString buffer;
    if (!(in = popen(line.toLatin1().data(),"r"))){
        qDebug() << "Open Failed using line: " + line;
        return result;
    } else {
        while (fgets(buff,sizeof(buff),in) != NULL) {
            buffer = buff;
            result += buffer;
        }
        pclose(in);
    }
    return result;
}

int WijiUiScriptable::newDialog(QString url, int width, int height) {
    WijiDialog* d = new WijiDialog();
    d->setSize(width,height);
    d->setUrl(url);
    d->show();
    dialogs.append(d);
    int lastindex = dialogs.length() - 1;
    return lastindex;
}
bool WijiUiScriptable::destroyDialog(int index) {
    WijiDialog* d = dialogs.takeAt(index);
    d->close();
    dialogs.removeAt(index);
    return true;
}
bool WijiUiScriptable::resizeDialog(int index, int width, int height) {
    WijiDialog* d = dialogs.takeAt(index);
    d->setSize(width,height);
    return true;
}
bool WijiUiScriptable::setDialogUrl(int index,QString url) {
    WijiDialog* d = dialogs.takeAt(index);
    d->setUrl(url);
    return true;
}
bool WijiUiScriptable::consoleLog(QString text) {
    qDebug() << text;
    return true;
}

bool WijiUiScriptable::setMainWindowSize(int width, int height) {
    QMetaObject::invokeMethod(WijiUi::getSelf(), "setWindowSize",
                              Qt::AutoConnection,
                              Q_ARG(int, width),
                              Q_ARG(int, height));
    return true;
}
QString WijiUiScriptable::testServer(QString message) {
    return QString("Hello from wiji, your message was: " + message);
}
QString WijiUiScriptable::getElementById(QString id) {
    QWebElement item = WijiUi::getSelf()->webView->page()->currentFrame()->findFirstElement("#" + id);
    QString result = item.toOuterXml();
    return result;
}
QString WijiUiScriptable::getContentOfElementById(QString id) {
    QWebElement item = WijiUi::getSelf()->webView->page()->currentFrame()->findFirstElement("#" + id);
    QString result = item.toInnerXml();
    return result;
}
bool WijiUiScriptable::replaceElement(QString id, QString content) {
    QWebElement item = WijiUi::getSelf()->webView->page()->currentFrame()->findFirstElement("#" + id);
    if ( !item.isNull() ) {
        item.setOuterXml(content);
        return true;
    }
    return false;
}
QString WijiUiScriptable::saveFileDialog(QString title, QString path, QString fileHint) {
    QString fileName = QFileDialog::getSaveFileName(0, title,
                                path + "/" + fileHint);
    return fileName;
}
QString WijiUiScriptable::openFileDialog(QString title, QString path) {
    QString fileName = QFileDialog::getOpenFileName(0,title,path);
    return fileName;
}
QString WijiUiScriptable::openFilesDialog(QString title, QString path) {
    QStringList fileNames = QFileDialog::getOpenFileNames(0,title,path);
    QString fileList = "WijiArray:" + fileNames.join("|||");
    return fileList;
}
bool WijiUiScriptable::setMainWindowHtml(QString content, QString url) {
    QMetaObject::invokeMethod(WijiUi::getSelf(), "setHtml",
                              Qt::AutoConnection,
                              Q_ARG(QString, content),
                              Q_ARG(QString, url));
    return true;
}
